<!doctype html>
<html>
   <head>
      <title>Embed Container for easyXDM</title>
      <script type="text/javascript" src="js/easyXDM/easyXDM.min.js"></script>
      <script type="text/javascript">
//         document.domain = document.domain;
         
         easyXDM.DomHelper.requiresJSON("js/easyXDM/json2.js");
         
         var callRemote = function(func, args, callback) {
            var options = { func: func, args: args, success: success, failure: failure, id: Math.floor(Math.random()*4294967295) };
            this.postMessage(JSON.stringify(options));
         };
         
         var callRemote = function(func, args, callback) {
            var options = { func: func, args: args };

            if (callback) {
               options.callbackID = generateCbid();
               callbacks[options.callbackID] = callback;
            }

            socket.postMessage(JSON.stringify(options));
         };
         
         var callback = function(callbackID, args) {
//            if (callbacks[callbackID] == undefined) {
//               Vanilla.error("Unkown callback ID: "+callbackID);
//            }

            args = args || [];
            if ((typeof args) == 'string')
               args = [args];
            callbacks[callbackID].apply(this, args)

            delete callbacks[callbackID];
         }
         
         // Generates a random ID for use as a callback id
         var generateCbid = function() {
            var genRand = function(){
               return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
            };
            return genRand() + genRand();
         };

         // Object containing callbacks
         var iframe, callbacks = {};
         
         function hide() {
            iframe.style.visibility = "hidden";
         }
         function show() {
            iframe.style.visibility = "visible"; 
         };
            
         var socket = new easyXDM.Socket({
            swf: "js/easyXDM/easyxdm.swf",
            onReady: function(){
               iframe = document.createElement("iframe");
               iframe.id = 'vanilla-iframe';
               iframe.frameBorder = 0;
               iframe.allowtransparency = true;
               iframe.scrolling = "no";
               iframe.style.visibility = "hidden";
               
               document.body.appendChild(iframe);
               
               iframe.src = easyXDM.query.url;
               
//               if(iframe.addEventListener) {
//                  iframe.addEventListener('DOMContentLoaded', loaded, true);
//               } else if(iframe.attachEvent) {
//                  iframe.attachEvent('onreadystatechange', loaded);
//               } else
//                  loaded();
            },
            onMessage: function(message, origin) {
               var data = JSON.parse(message);
               var func = iframe.contentWindow.Vanilla.parent[data.func];

               data.args = data.args || [];
               if ((typeof data.args) == 'string')
                  data.args = [data.args];
               
               if (data.func == 'setLocation') {
                  iframe.src = data.args[0];
                  return;
               } else if (data.func == 'callback') {
                  var callbackID = data.args[0];
                  var args = data.args[1];
                  if (!args)
                     args = [];
                  
                  callbacks[callbackID].apply(this, data.args[1]);
                  return;
               }
               
               if ((typeof func) != 'function')
                  throw new Error(data.func+' is not a function.');
               
               if (data.callbackID) {
                  // The function was called with a callback.
                  var callback = function() {
                     callRemote("callback", [data.callbackID, Array.prototype.slice.call(arguments)]);
                  }

                  data.args.push(callback);
               }

               func.apply(this, data.args);
            }
         });
      </script>
      <style type="text/css">
         html, body {
            background-color:transparent;
            overflow: hidden;
            margin: 0px;
            padding: 0px;
            width: 100%;
            height: 100%;
         }

         iframe {
            width: 100%;
            height: 100%;
            border: 0px;
         }
      </style>
   </head>
   <body>
   </body>
</html>